cmake_minimum_required(VERSION 3.5)
project(bs_drogon CXX)

include(CheckIncludeFileCXX)

check_include_file_cxx(any HAS_ANY)
check_include_file_cxx(string_view HAS_STRING_VIEW)
check_include_file_cxx(coroutine HAS_COROUTINE)
if (NOT "${CMAKE_CXX_STANDARD}" STREQUAL "")
    # Do nothing
elseif (HAS_ANY AND HAS_STRING_VIEW AND HAS_COROUTINE)
    set(CMAKE_CXX_STANDARD 20)
elseif (HAS_ANY AND HAS_STRING_VIEW)
    set(CMAKE_CXX_STANDARD 17)
else ()
    set(CMAKE_CXX_STANDARD 14)
endif ()

set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_executable(${PROJECT_NAME} main.cc)

# 包含drogon源码编译
# add_subdirectory(drogon) 
# target_link_libraries(${PROJECT_NAME} PRIVATE drogon)

find_package(Drogon CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE Drogon::Drogon)

if (CMAKE_CXX_STANDARD LESS 17)
    # With C++14, use boost to support any, string_view and filesystem
    message(STATUS "use c++14")
    find_package(Boost 1.61.0 REQUIRED)
    target_link_libraries(${PROJECT_NAME} PUBLIC Boost::boost)
elseif (CMAKE_CXX_STANDARD LESS 20)
    message(STATUS "use c++17")
else ()
    message(STATUS "use c++20")
endif ()

# 查找路径下所有源文件
aux_source_directory(controllers CTL_SRC)
aux_source_directory(filters FILTER_SRC)
aux_source_directory(plugins PLUGIN_SRC)
aux_source_directory(models MODEL_SRC)
aux_source_directory(tasks TASK_SRC)
aux_source_directory(utils UTIL_SRC)

target_include_directories(${PROJECT_NAME}
                           PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
                                   ${CMAKE_CURRENT_SOURCE_DIR}/models)

target_sources(${PROJECT_NAME}
               PRIVATE
               ${SRC_DIR}
               ${CTL_SRC}
               ${FILTER_SRC}
               ${PLUGIN_SRC}
               ${MODEL_SRC}
               ${TASK_SRC}
               ${UTIL_SRC})
            
# 测试模块
add_subdirectory(test)

# 定义目标路径
set(TARGET_DIR "${CMAKE_BINARY_DIR}/bin")

# 定义源文件路径
set(SRC_FILES "${CMAKE_SOURCE_DIR}/config.json" "${CMAKE_SOURCE_DIR}/index.html")
set(SRC_DIRS "${CMAKE_SOURCE_DIR}/public")

# 将文件复制到build
if(CMAKE_HOST_WIN32)
    add_custom_command(
        TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/Debug/logs  # 创建logs
        COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/Debug/public  # 创建logs
        COMMAND ${CMAKE_COMMAND} -E copy ${SRC_FILES} ${CMAKE_BINARY_DIR}/Debug
        COMMAND ${CMAKE_COMMAND} -E copy_directory ${SRC_DIRS} ${CMAKE_BINARY_DIR}/Debug/public  # 复制public目录
        COMMENT "Copying config.json, index.html, public to ${CMAKE_BINARY_DIR}"
    )
elseif(CMAKE_HOST_APPLE)
    add_custom_command(
        TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/logs  # 创建logs
        COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/public  # 创建logs
        COMMAND ${CMAKE_COMMAND} -E copy ${SRC_FILES} ${CMAKE_BINARY_DIR}
        COMMAND ${CMAKE_COMMAND} -E copy_directory ${SRC_DIRS} ${CMAKE_BINARY_DIR}/public  # 复制public目录
        COMMENT "Copying config.json, index.html, public to ${CMAKE_BINARY_DIR}"
    )
endif()


# 将文件复制到bin
if(CMAKE_HOST_WIN32)
    message("win")
elseif(CMAKE_HOST_APPLE)
    add_custom_command(
        TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E make_directory ${TARGET_DIR}  # 创建bin
        COMMAND ${CMAKE_COMMAND} -E make_directory ${TARGET_DIR}/logs  # 创建logs
        COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_NAME} ${TARGET_DIR}
        COMMAND ${CMAKE_COMMAND} -E copy ${SRC_FILES} ${TARGET_DIR}
        COMMAND ${CMAKE_COMMAND} -E copy_directory ${SRC_DIRS} ${TARGET_DIR}/public  # 复制public目录
        COMMENT "Copying config.json, index.html, public to ${TARGET_DIR}"
    )
endif()